class Solution {
public:
    int maxSubarrayLength(vector<int>& nums, int k) {
        int ans=0;
        unordered_map<int,int> hash;
        for(int left=0,right=0;right<nums.size();++right)
        {
            ++hash[nums[right]];
            while(left<right&&hash[nums[right]]>k)
            {
                --hash[nums[left]];
                ++left;
            }
            ans=max(ans,right-left+1);
        }
        return ans;
    }
};